<?php
/**
 * Project:     Rebel PHP Framework
 * File:        Autoload.php
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * @link http://www.rebelphpframework.org
 * @copyright 2007 Emanuele Colonnelli
 * @author Emanuele Colonnelli <emanuele.colonnelli at gmail dot com>
 * @package kernel
 * @version 1.0
 */

if(define(__FILE__,true)) {
	
	class Rebel_Net_Http_Request {
		
		const 	REQUEST = '_REQUEST',
				POST = '_POST',
				COOKIE = '_COOKIE',
				GET = '_GET';
		
		/**
		 * Shortcut to get GPC variables
		 *
		 * @param unknown_type $var
		 * @param unknown_type $type
		 * @return unknown
		 */
		public static function get($var,$type = self::REQUEST) {
			
			$targetVal = isset($GLOBALS[$type]) ? $GLOBALS[$type] : null;
			$path = self::parsePathString($var);
			
			if(1 < count($path)) {
				
				if(!$val = $targetVal[array_shift($path)]) {
					return null;
				}
				/**
				 * trick to avoid treat numeric hash keys as string
				 */
				while($t = array_shift($path)) {
					if(preg_match('/^#(\d+)/',$t,$ret)) {
						$val = $val[$ret[1]];
					} else {
						$val = $val[$t];
					}
				}
			} else {
				$val = isset($targetVal[$path[0]]) ? $targetVal[array_shift($path)] : null;
			}
			return $val;
		}//:-
		
		/**
		 * Parses a chain of nested GCPS array keys
		 *
		 * @param $strPath
		 * @param $separator
		 * @return unknown
		 */
		private static function parsePathString($strPath,$separator='.') {
			
			$t = explode($separator,$strPath);
			return array_map(array('Rebel_Net_Http_Request','_parse'),$t);
			
		}//:-
		
		private static function _parse ($s){
				/**
				 * trick to avoid treat numeric hash keys as string
				 */
				return is_numeric($s) ? "#$s" : $s;
		}
		
	}///:-
}
?>